﻿using System;
using System.Windows.Forms;

namespace Microsoft.Office.PowerPoint.STAMP.Core
{
    /// <summary>
    /// A progress observer with a form that will update it.
    /// </summary>
    public abstract class ProgressObserverWithForm : IProgressObserver
    {
        private readonly IWin32Window _parentForm;
        private readonly Progress _formProgress;

        /// <summary>
        /// Initializes a new instance of the <see cref="CreationProgressObserverWithForm"/> class.
        /// </summary>
        /// <param name="parentForm">The parent form.</param>
        /// <param name="captionCount">The caption count.</param>
        protected ProgressObserverWithForm(IWin32Window parentForm, int captionCount)
        {
            _parentForm = parentForm;
            _formProgress = new Progress(captionCount);
            _formProgress.Show(_parentForm);
        }

        /// <summary>
        /// Updates the observers current index.
        /// </summary>
        /// <param name="progressIndex">Index of the current completed item progress.</param>
        public void Update(int progressIndex)
        {
            if (progressIndex % 10 == 0)
            {
                UpdateForm(progressIndex, _formProgress);
            }
        }

        protected abstract void UpdateForm(int progressIndex, Progress progressForm);

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            _formProgress.Close();
            _formProgress.Dispose();
        }
    }

    /// <summary>
    /// An observer that can be updated with the index of the item currently being worked on.
    /// The progress will be displayed n a <see cref="Progress"/> form.
    /// </summary>
    public class CreationProgressObserverWithForm : ProgressObserverWithForm
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="CreationProgressObserverWithForm"/> class.
        /// </summary>
        /// <param name="parentForm">The parent form.</param>
        /// <param name="captionCount">The caption count.</param>
        public CreationProgressObserverWithForm(IWin32Window parentForm, int captionCount) : base(parentForm, captionCount)
        {
        }

        protected override void UpdateForm(int progressIndex, Progress progressForm)
        {
            progressForm.UpdateCreating(progressIndex);
        }
    }

    /// <summary>
    /// An observer that can be updated with the index of the item currently being worked on.
    /// The progress will be displayed n a <see cref="Progress"/> form.
    /// </summary>
    public class DeletingProgressObserverWithForm : ProgressObserverWithForm
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="CreationProgressObserverWithForm"/> class.
        /// </summary>
        /// <param name="parentForm">The parent form.</param>
        /// <param name="captionCount">The caption count.</param>
        public DeletingProgressObserverWithForm(IWin32Window parentForm, int captionCount) : base(parentForm, captionCount)
        {
        }

        protected override void UpdateForm(int progressIndex, Progress progressForm)
        {
            progressForm.UpdateDeleting(progressIndex);
        }
    }
}